package com.example.tabletexample;

import com.example.tabletexample.ItemListFragment.ItemListFragmentListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.FrameLayout;

public class MainActivity extends Activity implements ItemListFragmentListener {

	private boolean isTablet = false;
	private FrameLayout frameLeft;
	private FrameLayout frameRight;
	private Context mContext;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		long time = System.currentTimeMillis();
			
		setContentView(R.layout.fragment_container_layout);
		
		mContext = this;
		
		frameLeft = (FrameLayout) findViewById(R.id.container_left);
		frameRight = (FrameLayout) findViewById(R.id.container_right);
		
		//frameRight will be null if this is not run on a tablet
		//because findViewById(R.id.container_right) will not
		//be able to find container_right
		if(frameRight != null){
			isTablet = true;
		}
		
		getFragmentManager()
				.beginTransaction()
				.replace(R.id.container_left, new ItemListFragment(this))
				.commit();
		
		Log.d("timing","oncreate took "+(System.currentTimeMillis()-time));
		
	}

	@Override
	public void onItemSelected(String item) {
		//Check if this is run on a tablet
		if(isTablet){
			//This is a tablet,
			//Put the details on the right pane
			
			//Create a TextFragment with the 
			//appropriate text
			TextFragment details = new TextFragment();
			Bundle args = new Bundle();
			args.putString("text", item + " was pressed."+
									" Here are details of it");
			details.setArguments(args);
			
			//Put the TextFragment into the
			//container on the right
			getFragmentManager()
				.beginTransaction()
				.replace(R.id.container_right, details)
				.commit();
		}else{
			//This is a phone,
			//Start a new Activity to
			//display the details
			
			//Create a TextFragment with the 
			//appropriate text
			TextFragment details = new TextFragment();
			Bundle args = new Bundle();
			args.putString("text", item + " was pressed."+
									" Here are details of it");
			details.setArguments(args);
			
			//Put the TextFragment into the
			//container on the right,
			//also, add it to the back stack
			//so when the user presses the back button
			//this Fragment will be removed
			//and the ListFragment will be shown
			getFragmentManager()
				.beginTransaction()
				.addToBackStack(null)
				.replace(R.id.container_left, details)
				.commit();
		}
		
	}

}
